
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>002-雪糕的最大数量 · Dragon GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Bruse-Lee">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="Sum.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://tinytalking.blackdragonlee.com/login" target="_blank" class="custom-link">MyTinyTalk</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        <li class="header">简介</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">My Life👓</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../Family.html">
            
                <a href="../Family.html">
            
                    
                    👦🏼全家福
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../ListBook.html">
            
                <a href="../ListBook.html">
            
                    
                    百科全书📕
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../Basketball.html">
            
                <a href="../Basketball.html">
            
                    
                    篮球🤾🏾‍♂️所带给我的一切
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../secret.html">
            
                <a href="../secret.html">
            
                    
                    与老友🧙🏼‍♂️彻夜长谈的秘密
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">My Technical Summary🧰</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../book.html">
            
                <a href="../book.html">
            
                    
                    Gitbook的那些事🌟
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../FileOnline.html">
            
                <a href="../FileOnline.html">
            
                    
                    💥项目上线的那些事
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../lock.html">
            
                <a href="../lock.html">
            
                    
                    🤔悲观锁和😀乐观锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ShareHand.html">
            
                <a href="../ShareHand.html">
            
                    
                    三次握手🤝,四次挥手👋得这么讲
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../RemoveRestrictions.html">
            
                <a href="../RemoveRestrictions.html">
            
                    
                    解除网盘下载速度限制🚀
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../Bilibili.html">
            
                <a href="../Bilibili.html">
            
                    
                    📺小破站的爱恨情仇
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">My Technical Document⏳</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../CSharp/">
            
                <a href="../CSharp/">
            
                    
                    C#设计模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="../CSharp/Singleton.html">
            
                <a href="../CSharp/Singleton.html">
            
                    
                    第一节-单例模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.2" data-path="../CSharp/Factory.html">
            
                <a href="../CSharp/Factory.html">
            
                    
                    第二节-简单工程和工厂方法模式
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    Redis缓存三大问题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.2.1" data-path="../Redis/penetrate.html">
            
                <a href="../Redis/penetrate.html">
            
                    
                    001-缓存穿透
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.2" data-path="../Redis/breakdown.html">
            
                <a href="../Redis/breakdown.html">
            
                    
                    002-缓存击穿
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2.3" data-path="../Redis/avalanche.html">
            
                <a href="../Redis/avalanche.html">
            
                    
                    003-缓存雪崩
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">My Practice Topic</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="./">
            
                <a href="./">
            
                    
                    LeetCode刷题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="Sum.html">
            
                <a href="Sum.html">
            
                    
                    001-两数之和
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="5.1.2" data-path="Ice.html">
            
                <a href="Ice.html">
            
                    
                    002-雪糕的最大数量
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >002-雪糕的最大数量</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="002-&#x96EA;&#x7CD5;&#x7684;&#x6700;&#x5927;&#x6570;&#x91CF;">002-&#x96EA;&#x7CD5;&#x7684;&#x6700;&#x5927;&#x6570;&#x91CF;</h1>
<p><strong>&#x590F;&#x65E5;&#x708E;&#x708E;&#xFF0C;&#x5C0F;&#x7537;&#x5B69; Tony &#x60F3;&#x4E70;&#x4E00;&#x4E9B;&#x96EA;&#x7CD5;&#x6D88;&#x6D88;&#x6691;&#x3002;</strong></p>
<p><strong>&#x5546;&#x5E97;&#x4E2D;&#x65B0;&#x5230; n &#x652F;&#x96EA;&#x7CD5;&#xFF0C;&#x7528;&#x957F;&#x5EA6;&#x4E3A; n &#x7684;&#x6570;&#x7EC4; costs &#x8868;&#x793A;&#x96EA;&#x7CD5;&#x7684;&#x5B9A;&#x4EF7;&#xFF0C;&#x5176;&#x4E2D; costs[i] &#x8868;&#x793A;&#x7B2C; i &#x652F;&#x96EA;&#x7CD5;&#x7684;&#x73B0;&#x91D1;&#x4EF7;&#x683C;&#x3002;Tony &#x4E00;&#x5171;&#x6709; coins &#x73B0;&#x91D1;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x6D88;&#x8D39;&#xFF0C;&#x4ED6;&#x60F3;&#x8981;&#x4E70;&#x5C3D;&#x53EF;&#x80FD;&#x591A;&#x7684;&#x96EA;&#x7CD5;&#x3002;</strong></p>
<p><strong>&#x7ED9;&#x4F60;&#x4EF7;&#x683C;&#x6570;&#x7EC4; costs &#x548C;&#x73B0;&#x91D1;&#x91CF; coins &#xFF0C;&#x8BF7;&#x4F60;&#x8BA1;&#x7B97;&#x5E76;&#x8FD4;&#x56DE; Tony &#x7528; coins &#x73B0;&#x91D1;&#x80FD;&#x591F;&#x4E70;&#x5230;&#x7684;&#x96EA;&#x7CD5;&#x7684; &#x6700;&#x5927;&#x6570;&#x91CF; &#x3002;</strong></p>
<p><strong>&#x6CE8;&#x610F;&#xFF1A;Tony &#x53EF;&#x4EE5;&#x6309;&#x4EFB;&#x610F;&#x987A;&#x5E8F;&#x8D2D;&#x4E70;&#x96EA;&#x7CD5;&#x3002;</strong></p>
<p><strong>&#x793A;&#x4F8B; 1&#xFF1A;</strong></p>
<pre><code>&#x8F93;&#x5165;&#xFF1A;costs = [1,3,2,4,1], coins = 7
&#x8F93;&#x51FA;&#xFF1A;4
&#x89E3;&#x91CA;&#xFF1A;Tony &#x53EF;&#x4EE5;&#x4E70;&#x4E0B;&#x6807;&#x4E3A; 0&#x3001;1&#x3001;2&#x3001;4 &#x7684;&#x96EA;&#x7CD5;&#xFF0C;&#x603B;&#x4EF7;&#x4E3A; 1 + 3 + 2 + 1 = 7
</code></pre><p><strong>&#x793A;&#x4F8B; 2&#xFF1A;</strong></p>
<pre><code>&#x8F93;&#x5165;&#xFF1A;costs = [10,6,8,7,7,8], coins = 5
&#x8F93;&#x51FA;&#xFF1A;0
&#x89E3;&#x91CA;&#xFF1A;Tony &#x6CA1;&#x6709;&#x8DB3;&#x591F;&#x7684;&#x94B1;&#x4E70;&#x4EFB;&#x4F55;&#x4E00;&#x652F;&#x96EA;&#x7CD5;&#x3002;
</code></pre><p><strong>&#x793A;&#x4F8B; 3&#xFF1A;</strong></p>
<pre><code>&#x8F93;&#x5165;&#xFF1A;costs = [1,6,3,1,2,5], coins = 20
&#x8F93;&#x51FA;&#xFF1A;6
&#x89E3;&#x91CA;&#xFF1A;Tony &#x53EF;&#x4EE5;&#x4E70;&#x4E0B;&#x6240;&#x6709;&#x7684;&#x96EA;&#x7CD5;&#xFF0C;&#x603B;&#x4EF7;&#x4E3A; 1 + 6 + 3 + 1 + 2 + 5 = 18 &#x3002;
</code></pre><p><strong>&#x63D0;&#x793A;&#xFF1A;</strong></p>
<pre><code>costs.length == n
1 &lt;= n &lt;= 105
1 &lt;= costs[i] &lt;= 105
1 &lt;= coins &lt;= 108
</code></pre><h2 id="&#x9898;&#x89E3;">&#x9898;&#x89E3;</h2>
<h3 id="&#x65B9;&#x6CD5;&#xFF1A;&#x6392;&#x5E8F;&#x8D2A;&#x5FC3;">&#x65B9;&#x6CD5;&#xFF1A;&#x6392;&#x5E8F;+&#x8D2A;&#x5FC3;</h3>
<p><strong>&#x5728;&#x7ED9;&#x5B9A;&#x786C;&#x5E01;&#x6570;&#x91CF; \textit{coins}coins &#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x8981;&#x4E70;&#x5230;&#x6700;&#x591A;&#x7684;&#x96EA;&#x7CD5;&#xFF0C;&#x5E94;&#x8BE5;&#x4E70;&#x6700;&#x4FBF;&#x5B9C;&#x7684;&#x96EA;&#x7CD5;&#xFF0C;&#x7406;&#x7531;&#x5982;&#x4E0B;&#x3002;</strong></p>
<blockquote>
<p>&#x5047;&#x8BBE;&#x8D2D;&#x4E70;&#x6700;&#x4FBF;&#x5B9C;&#x7684;&#x96EA;&#x7CD5;&#xFF0C;&#x5728;&#x603B;&#x4EF7;&#x683C;&#x4E0D;&#x8D85;&#x8FC7; \textit{coins}coins &#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x6700;&#x591A;&#x53EF;&#x4EE5;&#x8D2D;&#x4E70; kk &#x652F;&#x96EA;&#x7CD5;&#x3002;&#x5982;&#x679C;&#x5C06; kk &#x652F;&#x6700;&#x4FBF;&#x5B9C;&#x7684;&#x96EA;&#x7CD5;&#x4E2D;&#x7684;&#x4EFB;&#x610F;&#x4E00;&#x652F;&#x96EA;&#x7CD5;&#x66FF;&#x6362;&#x6210;&#x53E6;&#x4E00;&#x652F;&#x96EA;&#x7CD5;&#xFF0C;&#x5219;&#x66FF;&#x6362;&#x540E;&#x7684;&#x96EA;&#x7CD5;&#x7684;&#x4EF7;&#x683C;&#x5927;&#x4E8E;&#x6216;&#x7B49;&#x4E8E;&#x66FF;&#x6362;&#x524D;&#x7684;&#x96EA;&#x7CD5;&#x7684;&#x4EF7;&#x683C;&#xFF0C;&#x56E0;&#x6B64;&#x66FF;&#x6362;&#x540E;&#x7684;&#x603B;&#x4EF7;&#x683C;&#x5927;&#x4E8E;&#x6216;&#x7B49;&#x4E8E;&#x66FF;&#x6362;&#x524D;&#x7684;&#x603B;&#x4EF7;&#x683C;&#xFF0C;&#x5141;&#x8BB8;&#x8D2D;&#x4E70;&#x7684;&#x96EA;&#x7CD5;&#x6570;&#x91CF;&#x4E0D;&#x53EF;&#x80FD;&#x8D85;&#x8FC7; kk&#x3002;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x4E70;&#x5230;&#x7684;&#x96EA;&#x7CD5;&#x7684;&#x6700;&#x5927;&#x6570;&#x91CF;&#x4E3A; kk&#x3002;</p>
</blockquote>
<p><strong>&#x7531;&#x6B64;&#x53EF;&#x4EE5;&#x5F97;&#x5230;&#x8D2A;&#x5FC3;&#x7684;&#x89E3;&#x6CD5;&#xFF1A;&#x5BF9;&#x6570;&#x7EC4; \textit{costs}costs &#x6392;&#x5E8F;&#xFF0C;&#x7136;&#x540E;&#x6309;&#x7167;&#x4ECE;&#x5C0F;&#x5230;&#x5927;&#x7684;&#x987A;&#x5E8F;&#x904D;&#x5386;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#xFF0C;&#x5BF9;&#x4E8E;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x5982;&#x679C;&#x8BE5;&#x5143;&#x7D20;&#x4E0D;&#x8D85;&#x8FC7;&#x5269;&#x4F59;&#x7684;&#x786C;&#x5E01;&#x6570;&#xFF0C;&#x5219;&#x5C06;&#x786C;&#x5E01;&#x6570;&#x51CF;&#x53BB;&#x8BE5;&#x5143;&#x7D20;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x8D2D;&#x4E70;&#x4E86;&#x8FD9;&#x652F;&#x96EA;&#x7CD5;&#xFF0C;&#x5F53;&#x9047;&#x5230;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x8D85;&#x8FC7;&#x5269;&#x4F59;&#x7684;&#x786C;&#x5E01;&#x6570;&#x65F6;&#xFF0C;&#x7ED3;&#x675F;&#x904D;&#x5386;&#xFF0C;&#x6B64;&#x65F6;&#x8D2D;&#x4E70;&#x7684;&#x96EA;&#x7CD5;&#x6570;&#x91CF;&#x5373;&#x4E3A;&#x53EF;&#x4EE5;&#x8D2D;&#x4E70;&#x96EA;&#x7CD5;&#x7684;&#x6700;&#x5927;&#x6570;&#x91CF;&#x3002;</strong></p>
<pre><code>C#
public class Solution {
    public int MaxIceCream(int[] costs, int coins) {
        Array.Sort(costs); // &#x4ECE;&#x5C0F;&#x5230;&#x5927;&#x6392;&#x5E8F;
        int count = 0;
        int n = costs.Length;
        for (int i = 0; i &lt; n; i++) {
            int cost = costs[i];
            if (coins &gt;= cost) {
                coins -= cost;
                count++;
            } else {
                break;
            }
        }
        return count;
    }
}
</code></pre><footer class="page-footer"><span class="copyright">Copyright &#xA9; book.blackdragonlee.com 2021 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x5185;&#x5BB9;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF1A;
2021-07-02 12:33:48
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Sum.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: 001-两数之和">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"002-雪糕的最大数量","level":"5.1.2","depth":2,"previous":{"title":"001-两数之和","level":"5.1.1","depth":2,"path":"LeetCode/Sum.md","ref":"LeetCode/Sum.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","back-to-top-button","-lunr","-search","search-pro","splitter","tbfed-pagefooter","github","pageview-count","page-toc-button","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy book.blackdragonlee.com 2021","modify_label":"该内容更新时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"pluginsConfig":{"page-toc-button":{"maxTocDepth":2,"minTocSize":2}},"github":{"url":"https://github.com/Bruse-Lee"},"livereload":{},"splitter":{},"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"page-toc-button":{},"back-to-top-button":{},"pageview-count":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"Bruse-Lee","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dragon GitBook","language":"zh-hans","links":{"sidebar":{"MyTinyTalk":"http://tinytalking.blackdragonlee.com/login"}},"gitbook":"*","description":"记录日常学习的技术文档与生活趣事"},"file":{"path":"LeetCode/Ice.md","mtime":"2021-07-02T04:33:48.441Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-07-02T13:59:02.253Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

